import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建对象
 */
public class JsonObject {

    public static void main(String[] args) {
        Address address1 = new Address("上海", "奉贤", "10001");
        Address address2 = new Address("上海", "虹桥", "10002");
        Address address3 = new Address("杭州", "西湖", "20001");

        Person zhangsan = new Person("张三", 25, address1, new ArrayList<Person>());
        Person lisi = new Person("李四", 27, address2, new ArrayList<Person>());
        Person wangwu = new Person("王五", 24, address3, new ArrayList<Person>());

        zhangsan.friends.add(lisi);
        zhangsan.friends.add(wangwu);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String jsonString = gson.toJson(zhangsan);
        System.out.println(jsonString);
    }
}

class Address {
    String city;
    String state;
    String zip;

    public Address(String city, String state, String zip) {
        this.city = city;
        this.state = state;
        this.zip = zip;
    }
}

class Person {
    String name;
    int age;
    Address address;
    List<Person> friends;

    public Person(String name, int age, Address address, List<Person> friends) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.friends = friends;
    }
}
